<?php
// No direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.controller');

class OwnserviceController extends JController
{
	/**
	 * Display the view
	 *
	 * @access publish
	 * @return void
	*/
	function display() {
		
		global $mainframe;

		//Initialise variables
		$user		= & JFactory::getUser();
		
		// set a default view if none exists
		$view = JRequest::getCmd( 'view' );

		// view caching logic --sample ...are we logged in?
		if($user->get('id')){

			$Ownuser	= & Ownservice::getUser();
			$level = $Ownuser->get('level_name');

			if( strtolower($level) != 'member' ) {
				if(!($view) || ($view == 'ownservice')) {
					JRequest::setVar('view', 'transactions');
				}
			}
			parent::display(false);
		} else {
			//set view to login
			JRequest::setVar('layout', 'default');
			JRequest::setVar('view', 'login');
			parent::display(true);
		}
	}

	/**
	 * Logic to login 
	 *
	 * @access publish
	 * @return void
	*/
	function login() {

		// Check for request forgeries
		JRequest::checkToken('request') or jexit( 'Invalid Token' );

		global $mainframe;

		if ($return = JRequest::getVar('return', '', 'method', 'base64')) {
			$return = base64_decode($return);
			if (!JURI::isInternal($return)) {
				$return = '';
			}
		}

		$options = array();
		$options['remember'] = JRequest::getBool('remember', false);
		$options['return'] = $return;

		$credentials = array();
		$credentials['username'] = JRequest::getVar('username', '', 'method', 'username');
		$credentials['password'] = JRequest::getString('passwd', '', 'post', JREQUEST_ALLOWRAW);

		//preform the login action
		$error = $mainframe->login($credentials, $options);

		if(!JError::isError($error))
		{
			// Redirect if the return url is not registration or login
			if ( ! $return ) {
				$return	= JRoute::_('index.php?option=com_ownservice');
			}

			$mainframe->redirect( $return );
		}
		else
		{
			// Facilitate third party login forms
			if ( ! $return ) {
				$return	= JRoute::_('index.php?option=com_ownservice&view=login');
			}

			// Redirect to a login form
			$mainframe->redirect( $return );
		}
	}

	function changepassword() {
		// Create the view
		$view = & $this->getView('ownservice', 'html');

		// Get/Create the model
		$model = & $this->getModel('Ownservice');

		// Push the model into the view (as default)
		$view->setModel($model, true);

		// Set the layout
		$view->setLayout('changepassword');

		// Display the view
		$view->display();
	}

	function changepassword_save() {

		// Check for request forgeries
		JRequest::checkToken( 'request' ) or jexit( 'Invalid Token' );

		global $mainframe, $option;

		//Initialise variables
		$db		= & JFactory::getDBO();
		$acl	= & JFactory::getACL();
		$me		= & JFactory::getUser();
		$ownuser= & Ownservice::getUser();
		$app	= & JFactory::getApplication();

		// Create a new JUser object
		$user = new JUser($me->get('id'));

		if($user->get('id') < 1) {
			$msg = JText::_('No user');
			$this->setRedirect(JRoute::_('index.php?option=com_ownservice'));
			return;
		}

		// Get vars 
		$post = JRequest::get('post');
		$post['id']			= $user->get('id');
		$post['username']	= $user->get('username');
		$post['password']	= JRequest::getVar('password', '', 'post', 'string', JREQUEST_ALLOWRAW);
		$post['password2']	= JRequest::getVar('password2', '', 'post', 'string', JREQUEST_ALLOWRAW);

		if (!$user->bind($post)) {
			$mainframe->enqueueMessage(JText::_('OWNSERVICE_ERROR_SAVE_USER'), 'message');
			$mainframe->enqueueMessage($user->getError(), 'error');
			return $this->execute('changepassword');
		}

		/*
	 	 * Lets save the JUser object
	 	 */
		if (!$user->save()) {
			$mainframe->enqueueMessage(JText::_('OWNSERVICE_ERROR_SAVE_USER'), 'message');
			$mainframe->enqueueMessage($user->getError(), 'error');
			return $this->execute('changepassword');
		}

		// Get/Create the model
		$model = & $this->getModel('Member');

		$post['id']					= $ownuser->get('id');
		$post['changepassword']		= 1;
		/*
		 * lets save the Ownservice user object
		*/
		if(!$model->store($post)){
			$mainframe->enqueueMessage(JText::_('OWNSERVICE_ERROR_SAVE_USER'), 'message');
			$mainframe->enqueueMessage($user->getError(), 'error');
			return $this->execute('changepassword');
		}

		$ownuser->set('changepassword', 1);
		$session = &JFactory::getSession();
		$session->set('ownservice', $ownuser);

		$msg = JText::sprintf( 'SUCCESSFULLY SAVED CHANGES TO USER', $user->get('name') );

		$link = JRoute::_('index.php?option=com_ownservice');

		$this->setRedirect($link, $msg);
	}
}